<?php 
session_start();
class report extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Báo Cáo - Thống Kê";
            $this->_configs["authority"]="report";
            $this->_configs["MinAuthority"]=0;
            $this->load->model('admin-planners/report_model','report_model');
            if($this->checkauthority()<0){AccessDenied();}
        }
        
        public function index()
	{
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->assign('configs', $this->_configs);
            $this->smarty->view('admin-planners/report/00_report','contentofview');
            $this->smarty->view('admin-planners/00_1_styles','styles');
            $this->smarty->view('admin-planners/00_2_scripts','scripts');
            $this->smarty->view('admin-planners/00_3_menus','menus');
            $this->smarty->view('admin-planners/10_template','view');
            $this->smarty->display('admin-planners/01_manage');
            
	}
        public function synthesis($type="month",$value1=null,$value2=null){
            switch ($type){
                case "month":
                    $month=isset($value1)?$value1:date("m");
                    $configs["month"]=$month;
                    $year=isset($value2)?$value2:date("Y");
                    $configs["year"]=$year;
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo tháng";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/06_synthesis_month_product','contentofview');
                    break;
                case "quarter":
                    $quarter=isset($value1)?$value1:(int)((date("m")+3)/4);
                    $configs["quarter"]=$quarter;
                    $year=isset($value2)?$value2:date("Y");
                    $configs["year"]=$year;
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo quí";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/07_synthesis_quarter_product','contentofview');
                    break;
                case "year":
                    $year=isset($value2)?$value2:date("Y");
                    $configs["year"]=$year;
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo năm";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/08_synthesis_year_product','contentofview');
                    break;
                case "from_to":
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo thời gian ( ngày hôm nay )";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/09_synthesis_from_to_product','contentofview');
                    break;
            }
            
            $this->smarty->view('admin-planners/00_1_styles','styles');
            $this->smarty->view('admin-planners/00_2_scripts','scripts');
            $this->smarty->view('admin-planners/00_3_menus','menus');
            $this->smarty->view('admin-planners/10_template','view');
            $this->smarty->display('admin-planners/01_manage');
        }
        public function synthesis_detail($type="month",$value1=null,$value2=null){
            switch ($type){
                case "month":
                    $month=isset($value1)?$value1:date("m");
                    $configs["month"]=$month;
                    $year=isset($value2)?$value2:date("Y");
                    $configs["year"]=$year;
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo tháng";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/10_synthesis_month_detail','contentofview');
                    break;
                case "quarter":
                    $quarter=isset($value1)?$value1:(int)((date("m")+3)/4);
                    $configs["quarter"]=$quarter;
                    $year=isset($value2)?$value2:date("Y");
                    $configs["year"]=$year;
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo quí";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/07_synthesis_quarter_product','contentofview');
                    break;
                case "year":
                    $year=isset($value2)?$value2:date("Y");
                    $configs["year"]=$year;
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo năm";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/08_synthesis_year_product','contentofview');
                    break;
                case "from_to":
                    $configs["title"]="Báo cáo tổng hợp doanh thu theo thời gian ( ngày hôm nay )";
                    $this->smarty->assign('_SESSION', $_SESSION);
                    $this->smarty->assign('configs', $configs);
                    $this->smarty->view('admin-planners/report/09_synthesis_from_to_product','contentofview');
                    break;
            }
            
            $this->smarty->view('admin-planners/00_1_styles','styles');
            $this->smarty->view('admin-planners/00_2_scripts','scripts');
            $this->smarty->view('admin-planners/00_3_menus','menus');
            $this->smarty->view('admin-planners/10_template','view');
            $this->smarty->display('admin-planners/01_manage');
        }
        function jqgrid_data_synthesis_month($month=null,$year=null){
            if($month==null)$month=date("m");
            if($year==null)$year=date("Y");
            $startdate = date("Y-m-d",mktime(0, 0, 0, $month, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, $month+1, 0,   $year));
            //echo "$startdate:$enddate";
            //            return;
            $resultdata=array();
            $result['total_rows']=0;
            if($this->checkauthority()>=0){
                $result=$this->report_model->gettotal_rows_synthesis_month($startdate,$enddate);
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $resultdata[] = array(
                            'ProductName'     => $row->ProductName,
                            'Supplier'=> $row->Supplier,
                            'Amount'=> $row->Amount,
                            'UnitPrice'     => $row->UnitPrice,
                            'TotalQuantity'=> $row->TotalQuantity,
                            'TotalOrderCash'=> $row->TotalOrderCash
                            
                    );
                }
            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $resultdata
            );
            echo json_encode($data);
        }
        function jqgrid_data_synthesis_quarter($quarter=1,$year=null){
            switch ($quarter){
                case 1:
                    $month=1;
                    break;
                case 2:
                    $month=4;
                    break;
                case 3:
                    $month=7;
                    break;
                case 4:
                    $month=10;
                    break;
                default :
                    $month=1;
            }
            if($year==null)$year=date("Y");
            $startdate = date("Y-m-d",mktime(0, 0, 0, $month, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, $month+3, 0,   $year));
            //echo "$startdate:$enddate";
            //            return;
            $resultdata=array();
            $result['total_rows']=0;
            if($this->checkauthority()>=0){
                $result=$this->report_model->gettotal_rows_synthesis_month($startdate,$enddate);
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $resultdata[] = array(
                            'ProductName'     => $row->ProductName,
                            'Supplier'=> $row->Supplier,
                            'Amount'=> $row->Amount,
                            'UnitPrice'     => $row->UnitPrice,
                            'TotalQuantity'=> $row->TotalQuantity,
                            'TotalOrderCash'=> $row->TotalOrderCash
                            
                    );
                }
            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $resultdata
            );
            echo json_encode($data);
        }
        function jqgrid_data_synthesis_year($year=null){
            if($year==null)$year=date("Y");
            $startdate = date("Y-m-d",mktime(0, 0, 0, 1, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, 12, 0,   $year));
            //echo "$startdate:$enddate";
            //            return;
            $resultdata=array();
            $result['total_rows']=0;
            if($this->checkauthority()>=0){
                $result=$this->report_model->gettotal_rows_synthesis_month($startdate,$enddate);
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $resultdata[] = array(
                            'ProductName'     => $row->ProductName,
                            'Supplier'=> $row->Supplier,
                            'Amount'=> $row->Amount,
                            'UnitPrice'     => $row->UnitPrice,
                            'TotalQuantity'=> $row->TotalQuantity,
                            'TotalOrderCash'=> $row->TotalOrderCash
                            
                    );
                }
            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $resultdata
            );
            echo json_encode($data);
        }
        function jqgrid_data_synthesis_from_to($from=null,$to=null){
            $startdate = $from;
            $enddate = $to;
            $resultdata=array();
            $result['total_rows']=0;
            if($this->checkauthority()>=0){
                $result=$this->report_model->gettotal_rows_synthesis_month($startdate,$enddate);
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $resultdata[] = array(
                            'ProductName'     => $row->ProductName,
                            'Supplier'=> $row->Supplier,
                            'Amount'=> $row->Amount,
                            'UnitPrice'     => $row->UnitPrice,
                            'TotalQuantity'=> $row->TotalQuantity,
                            'TotalOrderCash'=> $row->TotalOrderCash
                            
                    );
                }
            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $resultdata
            );
            echo json_encode($data);
        }
        function month(){
            $this->smarty->display('admin-planners/report/03_yeari');
        }
        function Chart_Spline_Series(){
            $this->smarty->display('admin-planners/report/04_Chart_Spline_Series');
        }
        function Line_Series(){
            $this->smarty->display('admin-planners/report/02_Line_Series');
        }
        function Stacked_Line_Series(){
            $this->smarty->display('admin-planners/report/05_Stacked_Line_Series');
        }
        public function yeardata(){
            for ($i=1;$i<=31;$i++) {
                    $customers[] = array(
                            'Date'  => "1/$i/2012",
                            'Referral'     => rand(1000, 1500),
                            'SearchPaid'     => rand(1000, 1500),
                            'SearchNonPaid'     => rand(1000, 1500)
                    );
                }
                echo json_encode($customers);
        }
        public function Line_Series_Data(){
            for ($i=1;$i<=31;$i++) {
                    $customers[] = array(
                            'Date'  => "1/$i/2012",
                            'S&P 500'     => rand(1000, 1500),
                            'NASDAQ'     => rand(1000, 1500),
                            'SearchNonPaid'     => rand(1000, 3000)
                    );
                }
                echo json_encode($customers);
        }
        public function checkauthority(){
            if(isset($_SESSION["ADP-USER"]["AUTHORITY"])){
                $myau=-1;
                if(is_array($_SESSION["ADP-USER"]["AUTHORITY"])){
                    foreach ($_SESSION["ADP-USER"]["AUTHORITY"] as $au){
                        if($au["keyword"]=="view" && $myau==-1)$myau=0;
                        if($au["keyword"]=="admin")$myau=1;
                        if($au["keyword"]==$this->_configs["authority"]){
                            if($au["value"]==0 && $myau==-1) $myau=0;
                            if($au["value"]==1) $myau=1;
                        }
                    }
                }
                return $myau>=$this->_configs["MinAuthority"]?$myau:-1;
            }return -1;
        }
        
        
        function ex(){
            header( "content-type: text/xml" );
            // Install the DB module using 'pear install DB'
            $month=date("m");
            $year=date("Y");
            $startdate = date("Y-m-d",mktime(0, 0, 0, $month, 1,   $year));
            $enddate = date("Y-m-d",mktime(0, 0, 0, $month+1, 0,   $year));
            $resultdata=array();
            $result=$this->report_model->gettotal_rows_synthesis_month($startdate,$enddate);
            $rows=$result['rows'];
            // get data and store in a json array
            foreach ($rows as $row) {
                $resultdata[] = array(
                        'ProductName'     => $row->ProductName,
                        'Supplier'=> $row->Supplier,
                        'Amount'=> $row->Amount,
                        'UnitPrice'     => $row->UnitPrice,
                        'TotalQuantity'=> $row->TotalQuantity,
                        'TotalOrderCash'=> $row->TotalOrderCash

                );
            }
            $rows = $resultdata;

            
            print "<?xml version=\"1.0\"?>\n";
            print "<?mso-application progid=\"Excel.Sheet\"?>\n";
            ?>
            <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
            xmlns:o="urn:schemas-microsoft-com:office:office"
            xmlns:x="urn:schemas-microsoft-com:office:excel"
            xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
            xmlns:html="http://www.w3.org/TR/REC-html40">
            <DocumentProperties 
                xmlns="urn:schemas-microsoft-com:office:office">
            <Author>Jack Herrington</Author>
            <LastAuthor>Jack Herrington</LastAuthor>
            <Created>2005-08-02T04:06:26Z</Created>
            <LastSaved>2005-08-02T04:30:11Z</LastSaved>
            <Company>My Company, Inc.</Company>
            <Version>11.6360</Version>
            </DocumentProperties>
            <ExcelWorkbook 
                xmlns="urn:schemas-microsoft-com:office:excel">
            <WindowHeight>8535</WindowHeight>
            <WindowWidth>12345</WindowWidth>
            <WindowTopX>480</WindowTopX>
            <WindowTopY>90</WindowTopY>
            <ProtectStructure>False</ProtectStructure>
            <ProtectWindows>False</ProtectWindows>
            </ExcelWorkbook>
            <Styles>
            <Style ss:ID="Default" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom"/>
            <Borders/>
            <Font/>
            <Interior/>
            <NumberFormat/>
            <Protection/>
            </Style>
            <Style ss:ID="s21" ss:Name="Hyperlink">
            <Font ss:Color="#0000FF" ss:Underline="Single"/>
            </Style>
            <Style ss:ID="s23">
            <Font x:Family="Swiss" ss:Bold="1"/>
            </Style>
            </Styles>
            <Worksheet ss:Name="Names">
            <Table ss:ExpandedColumnCount="4"
            ss:ExpandedRowCount="<?php echo( count( $rows ) + 1 ); ?>"
            x:FullColumns="1" x:FullRows="1">
            <Column ss:Index="4" ss:AutoFitWidth="0" ss:Width="154.5"/>
            <Row ss:StyleID="s23">
            <Cell><Data 
                ss:Type="String">First</Data></Cell>
            <Cell><Data 
            ss:Type="String">Middle</Data></Cell>
            <Cell><Data 
                ss:Type="String">Last</Data></Cell>
            <Cell><Data 
                ss:Type="String">Email</Data></Cell>
            </Row>
            <?php foreach( $rows as $row ) { ?>
            <Row>
            <Cell><Data 
                ss:Type="String"><?php echo( $row["ProductName"] ); ?>
            </Data></Cell>
            <Cell><Data 
                ss:Type="String"><?php echo( $row["ProductName"] ); ?>
            </Data></Cell>
            <Cell><Data 
                ss:Type="String"><?php echo( $row["ProductName"] ); ?>
            </Data></Cell>
            <Cell ss:StyleID="s21"><Data ss:Type="String">
            <?php echo( $row["ProductName"] ); ?></Data></Cell>
            </Row>
            <?php } ?>
            </Table>
            <WorksheetOptions 
                xmlns="urn:schemas-microsoft-com:office:excel">
            <Print>
            <ValidPrinterInfo/>
            <HorizontalResolution>300</HorizontalResolution>
            <VerticalResolution>300</VerticalResolution>
            </Print>
            <Selected/>
            <Panes>
            <Pane>
            <Number>3</Number>
            <ActiveRow>1</ActiveRow>
            </Pane>
            </Panes>
            <ProtectObjects>False</ProtectObjects>
            <ProtectScenarios>False</ProtectScenarios>
            </WorksheetOptions>
            </Worksheet>
            </Workbook>
            <?
        }
        
       
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */